package ltd.jdsoft.cute.common.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 字符串工具类
 * 
 * @author zhengzhq
 *
 */
public class StrTool {

  private static final String SEPERATOR_COMMA = ",";

  /**
   * 将字符串转为List<Long>返回
   * 
   * @param string
   * @return
   */
  public static List<Long> stringToLongList(String string) {
    if (string == null || string.length() == 0) {
      return new ArrayList<Long>();
    }

    // 按逗号分割
    String[] array = string.split(SEPERATOR_COMMA);
    List<Long> returnList = new ArrayList<Long>();
    for (String str : array) {
      returnList.add(Long.parseLong(str));
    }

    return returnList;
  }

  /**
   * 将数组用逗号隔开拼在一起
   * 
   * @param list
   * @return
   */
  public static String listToString(List<Long> list) {
    if (list == null || list.isEmpty()) {
      return "";
    }

    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < list.size(); i++) {
      if (i > 0) {
        stringBuffer.append(SEPERATOR_COMMA);
      }
      stringBuffer.append(list.get(i));
    }

    return stringBuffer.toString();
  }
}
